package com.ruoyi.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.LineGlobalConfig;
import com.ruoyi.system.domain.LineProduction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LineDevice;
import com.ruoyi.system.service.ILineDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/system/device")
public class LineDeviceController extends BaseController
{
    @Autowired
    private ILineDeviceService lineDeviceService;

    /**
     * 给其中一台设备发送MQTT消息
     *
     * @param lineDevice 设备
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/sendLineDeviceMessage")
    public AjaxResult sendLineDeviceMessage(@RequestBody LineDevice lineDevice,
                                            @RequestParam(value = "timeout", defaultValue = "10") Integer monitorTime,
                                            @RequestParam(value = "intervalInMs", defaultValue = "1000") Integer intervalInMs)  {
        int result;
        result = lineDeviceService.sendLineDeviceMessage(lineDevice, monitorTime, intervalInMs);
        return result == LineGlobalConfig.MSG_SUCCESS_CODE ? success("设备消息发送成功，共"+(int)(monitorTime*1000.0 / intervalInMs)+"条"):error("Error at NO."+result+" message");
    }

    /**
     * 给产线内所有设备发送MQTT消息
     *
     * @param lineProduction 产线
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/sendLineDeviceMessagesByProduction")
    public AjaxResult sendLineDeviceMessagesByProduction(@RequestBody LineProduction lineProduction,
                                                         @RequestParam(value = "timeout", defaultValue = "10") Integer monitorTime,
                                                         @RequestParam(value = "intervalInMs", defaultValue = "1000") Integer intervalInMs)  {
        int result;
        result = lineDeviceService.sendLineDeviceMessagesByProduction(lineProduction, monitorTime,intervalInMs);
        return result == LineGlobalConfig.MSG_SUCCESS_CODE ? success("产线消息发送成功，共"+(int)(monitorTime*1000.0 / intervalInMs)+"条"):error("Error at NO."+result+" message");
    }

    /**
     * 查询未删除设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(LineDevice lineDevice)
    {
        startPage();
        List<LineDevice> list = lineDeviceService.selectLineDeviceList(lineDevice);
        return getDataTable(list);
    }

    /**
     * 查询-已删除设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping("/delList")
    public TableDataInfo delList(LineDevice lineDevice)
    {
        startPage();
        List<LineDevice> list = lineDeviceService.selectLineDeviceDelList(lineDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LineDevice lineDevice)
    {
        List<LineDevice> list = lineDeviceService.selectLineDeviceList(lineDevice);
        ExcelUtil<LineDevice> util = new ExcelUtil<>(LineDevice.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息-根据设备主键
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(lineDeviceService.selectLineDeviceById(id));
    }

    /**
     * 查询设备详细信息列表-根据房间主键
     */
    @PreAuthorize("@ss.hasPermi('system:device:list')")
    @GetMapping(value = "/list/productionId/{id}")
    public TableDataInfo listProductionDeviceInfo
    (@PathVariable("id") Integer id, LineDevice lineDevice)
    {
        startPage();
        lineDevice.setProductionId(id);
        List<LineDevice> list = lineDeviceService.selectLineProductionDevicesByLineDevice(lineDevice);
        return getDataTable(list);
    }

    /**
     * 获取设备详细信息-根据设备名称（下次不要这么写，用对象多方便。。。）
     */
    @PreAuthorize("@ss.hasPermi('system:device:query')")
    @GetMapping(value = "/name/{name}")
    public AjaxResult getInfo(@PathVariable("name") String name)
    {
        return success(lineDeviceService.selectLineDeviceByName(name));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LineDevice lineDevice)
    {
        return toAjax(lineDeviceService.insertLineDevice(lineDevice));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('system:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LineDevice lineDevice)
    {
        return toAjax(lineDeviceService.updateLineDevice(lineDevice));
    }

    /**
     * 删除设备(del_flag赋值为1)
     */
    @PreAuthorize("@ss.hasPermi('system:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(lineDeviceService.deleteLineDeviceByIds(ids));
    }
    /**
     * 恢复设备
     * (del_flag赋值为0)[GET请求实现]
     */
    @PreAuthorize("@ss.hasPermi('system:device:add')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @GetMapping("/restore/{ids}")
    public AjaxResult restore(@PathVariable Integer[] ids)
    {
        return toAjax(lineDeviceService.restoreLineDeviceByIds(ids));
    }
}
